package com.lz.comm.image

import android.content.Context
import com.lz.comm.image.progress.ProgressGlideUrl
import com.lz.comm.image.progress.ProgressOkHttpUrlLoader
import com.lz.library.utils.FileUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.GlideBuilder
import com.bumptech.glide.Registry
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader
import com.bumptech.glide.load.engine.cache.DiskLruCacheFactory
import com.bumptech.glide.load.engine.cache.DiskLruCacheFactory.CacheDirectoryGetter
import com.bumptech.glide.load.model.GlideUrl
import com.bumptech.glide.load.model.stream.HttpGlideUrlLoader
import com.bumptech.glide.module.AppGlideModule
import okhttp3.OkHttpClient
import java.io.File
import java.io.InputStream

@GlideModule
class OkHttpAppGlideModule : AppGlideModule() {

    override fun applyOptions(context: Context, builder: GlideBuilder) {
        val cacheGetter = CacheDirectoryGetter { File(FileUtils.getCacheDir(), "imageCaches") }
        //设置sdk img路径及大小
        builder.setDiskCache(DiskLruCacheFactory(cacheGetter, 1024 * 1024 * 100))
    }

    override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
        val client = OkHttpClient()
        registry.replace(GlideUrl::class.java, InputStream::class.java, OkHttpUrlLoader.Factory(client))
        registry.append(ProgressGlideUrl::class.java, InputStream::class.java, ProgressOkHttpUrlLoader.Factory(client))
    }

    override fun isManifestParsingEnabled(): Boolean = false
}